package bupt.party.online.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import bupt.party.online.model.Admin;
import bupt.party.online.model.Branch;
import bupt.party.online.model.PartyNews;
import bupt.party.online.service.IAdminService;
import bupt.party.online.service.IBranchService;
import bupt.party.online.service.IPartyNewsService;
import bupt.party.online.util.SessionConstants;

public class PartyNewsAction extends Pagination implements ServletRequestAware,ServletResponseAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	HttpServletResponse response;
	private IPartyNewsService partyNewsService;
	private IAdminService adminService;
	private IBranchService branchService;
	
	//for page results
	private List<PartyNews> partyNews;
	
	//for college news
	private Integer branchId;
	private List<Branch> firstPaList = new ArrayList<Branch>();//党委列表
	private Integer firstPa = -1;
	
	//for pagination
	private String paginationCode;
	private String newsCategory;
	
	//for view news
	private int id;
	private PartyNews currentNews;
	
	//for manage Party news
	private String titleOnQuery;
	
	//for save party news
	private String title;
	private String content;
	
	//for pic upload
	private File newsPic;
	private String newsPicFileName;
	private String newsPicContentType;
	private static String RealNewsPicPath = ServletActionContext.getServletContext().getRealPath("/images/partyNewsImage");
	private static String ServerNewsPicPath = "/images/partyNewsImage";
	
	//for get user name
	private Map<String,String> adminNoToNameMap;
	
	//for branch tree html code
	private String branchTreeCode;
	
	
	//wangdong 
	private Integer tag=1;
	
	public String managePartyNews(){
		
		HttpSession session = request.getSession();
		
		Object user = session.getAttribute(SessionConstants.SESSION_USER);
		
		if(!(user instanceof Admin)) return "noPermission";
		
		Admin admin = (Admin) user;
		
		if(admin.getAdminLevel().equals(SessionConstants.ADMIN_THIRD_LEVEL)) return "redirectManageSpiritNews";
		
		if(admin.getAdminLevel().equals(SessionConstants.ADMIN_SECOND_LEVEL)) return "redirectManageCollegeNews";
		
		return "noPermission";
		
	}
	
	
	public String spiritNews(){
		
		if(pageNow == null) titleOnQuery="";
		
		Map<String,Object> conditions = new HashMap<String,Object>();
		
		if(this.titleOnQuery != null && this.titleOnQuery.trim() != "") conditions.put("title", "%"+titleOnQuery+"%");
		
		conditions.put("tag", PartyNews.Category_spirit_news);
		
		this.newsExecution(conditions);
		
		pageNow = null;
		
		request.setAttribute("titleOnQuery", titleOnQuery);
		
		request.setAttribute("actionName", "PartyNews_spiritNews");
		
		newsCategory="上级精神";
		
		branchTreeCode = this.branchService.getPageCode4BranchTree(true);
		
		return "spiritNews";
	}
	
	public String schoolNews(){
		
		if(pageNow == null) titleOnQuery="";
		
		Map<String,Object> conditions = new HashMap<String,Object>();
		
		if(this.titleOnQuery != null && this.titleOnQuery.trim() != "") conditions.put("title", "%"+titleOnQuery+"%");
		
		conditions.put("tag", PartyNews.Category_school_news);
		
		this.newsExecution(conditions);
		
		pageNow = null;
		
		request.setAttribute("titleOnQuery", titleOnQuery);		
		
		request.setAttribute("actionName", "PartyNews_schoolNews");
		
		newsCategory="学院要闻";
		
		branchTreeCode = this.branchService.getPageCode4BranchTree(true);
		
		return "schoolNews";
	}
	
	public String collegeNews(){
		
		if(pageNow == null){
			
			titleOnQuery="";
			
			pageNow = 1;
			
			branchTreeCode = this.branchService.getPageCode4BranchTree(true);
		} 
		
		if(branchId == null) branchId = 1;
		
		int focusBranchId = branchId;
		totalResults = 0;
		
		paginationCode = "";
		
		this.pageCount = (totalResults%pageSize == 0)?totalResults/pageSize : totalResults/pageSize + 1;
		
		List<Branch> branches = new ArrayList<Branch>();
		
		Branch parentBranch = new Branch();
		
		parentBranch.setId(branchId);
		
		this.branchService.getNodeChildrenBranches( parentBranch , branches);
		
		totalResults = this.partyNewsService.getCollegeNewsCount(branches, titleOnQuery);
		
		this.partyNews = this.partyNewsService.getCollegeNewsByBranch(branches, titleOnQuery, (pageNow-1)*pageSize, pageSize);
		
		Set<String> posterNoSet = new HashSet<String>();
		
		for(PartyNews news:partyNews){
			posterNoSet.add(news.getPosterNo());
		}
		
		adminNoToNameMap  = this.getNameByAdminNo(posterNoSet);
		
		if(totalResults > 0) paginationCode = Pagination.getPaginationCodeForTree(pageSize, totalResults, pageNow);
		
		else paginationCode = "没有查询到有效数据！";
		
		request.setAttribute("branchId", branchId);
		
		request.setAttribute("titleOnQuery", titleOnQuery);
		
		pageNow = null;
		
		branchId = null;
		
		List<Branch> level2Branches = this.branchService.getBranchLevel2();
		boolean flag = false;
		Iterator<Branch> iter = level2Branches.iterator();
		while(iter.hasNext()){
			if(iter.next().getId() == focusBranchId){
				flag = true;
				break;
			}
		}
		
		
		if(!flag && level2Branches != null && level2Branches.size() > 0){
//			focusBranchId = level2Branches.get(0).getId();//开始院系风采的时候默认的不是第一个列表的分党委，而是 设置默认的是：北邮党委
			focusBranchId = 1;
		}
		
		request.setAttribute("focusBranchId", focusBranchId);
		request.setAttribute("level2Branches", level2Branches);

		return "collegeNews";
		
	}
	
	public String pictureNews(){
		
		if(pageNow == null) titleOnQuery="";
		
		Map<String,Object> conditions = new HashMap<String,Object>();
		
		if(this.titleOnQuery != null && this.titleOnQuery.trim() != "") conditions.put("title", "%"+titleOnQuery+"%");
		
		conditions.put("picTag", 1);
		
		this.newsExecution(conditions);
		
		pageNow = null;
		
		request.setAttribute("titleOnQuery", titleOnQuery);		
		
		request.setAttribute("actionName", "PartyNews_schoolNews");
		
		newsCategory="图片新闻";
		
		return "pictureNews";

	}
	
	private void newsExecution(Map<String,Object> conditions){
		
		if(this.pageNow == null ) pageNow = 1;
		
		totalResults = 0;
		
		paginationCode = "";
		
		totalResults = this.partyNewsService.getResultCountByConditions(conditions);
		
		this.pageCount = (totalResults%pageSize == 0)?totalResults/pageSize : totalResults/pageSize + 1;
		
		partyNews = this.partyNewsService.getNewsByConditions(conditions, pageNow, pageSize);
		
		Set<String> posterNoSet = new HashSet<String>();
		
		for(PartyNews news:partyNews){
			posterNoSet.add(news.getPosterNo());
		}
		
		adminNoToNameMap  = this.getNameByAdminNo(posterNoSet);
		
		if(totalResults > 0) paginationCode = Pagination.getPaginationCode(pageSize, totalResults, pageNow);
		
		else paginationCode = "没有查询到有效数据！";
		
		request.setAttribute("modifyNewsPageNow", pageNow);
		
		pageNow = 1;
		
	}
	
	public String viewNews(){
		if(id < 0) id = 0;
		currentNews = null;
		currentNews = partyNewsService.getNewsById(id);
		//wangdong add a flag  
		
		int newsCategory = currentNews.getTag();//获取新闻类型的标志
		request.setAttribute("newsCategory", newsCategory);
		
		if(currentNews != null && currentNews.getVisitedCount() != null){
			
			currentNews.setVisitedCount(currentNews.getVisitedCount()+1);
			
		}else{
			currentNews.setVisitedCount(1);
		} 
		
		this.partyNewsService.saveOrUpdateSpiritNews(currentNews);
		
		return "viewNews";
	}
	
	public String manageSpiritNews(){
		
		if(!this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL)) return "noPermission";
		
		if(pageNow == null) titleOnQuery="";
		
		Map<String,Object> conditions = new HashMap<String,Object>();
		
		if(this.titleOnQuery != null && this.titleOnQuery.trim() != "") conditions.put("title", "%"+titleOnQuery+"%");
		
		conditions.put("tag", PartyNews.Category_spirit_news);
		
		this.newsExecution(conditions);
		
		pageNow = null;
		
		request.setAttribute("titleOnQuery", titleOnQuery);
		
		branchTreeCode = this.branchService.getPageCode4BranchTree(false);
		
		return "manageSpiritNews";
	}
	
	public String manageSchoolNews(){
		
		if(!this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL)) return "noPermission";
		
		if(pageNow == null) titleOnQuery="";
		
		Map<String,Object> conditions = new HashMap<String,Object>();
		
		if(this.titleOnQuery != null && this.titleOnQuery.trim() != "") conditions.put("title", "%"+titleOnQuery+"%");
		
		conditions.put("tag", PartyNews.Category_school_news);
		
		this.newsExecution(conditions);
		
		pageNow = null;
		
		request.setAttribute("titleOnQuery", titleOnQuery);
		
		branchTreeCode = this.branchService.getPageCode4BranchTree(false);
		
		return "manageSchoolNews";
	}
	
	public String manageCollegeNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		if(pageNow == null){
			
			titleOnQuery="";
			
			pageNow = 1;
			
		} 
		
		if(branchId == null) branchId = 1;
		
		int focusBranchId = branchId;
		
		totalResults = 0;
		
		paginationCode = "";
		
		this.pageCount = (totalResults%pageSize == 0)?totalResults/pageSize : totalResults/pageSize + 1;
		
		List<Branch> branches = new ArrayList<Branch>();
		
		Branch parentBranch = new Branch();
		
		parentBranch.setId(branchId);
		
		this.branchService.getNodeChildrenBranches( parentBranch , branches);
		
		totalResults = this.partyNewsService.getCollegeNewsCount(branches, titleOnQuery);
		
		this.partyNews = this.partyNewsService.getCollegeNewsByBranch(branches, titleOnQuery, (pageNow-1)*pageSize, pageSize);
		
		Set<String> posterNoSet = new HashSet<String>();
		
		for(PartyNews news:partyNews){
			posterNoSet.add(news.getPosterNo());
		}
		
		adminNoToNameMap  = this.getNameByAdminNo(posterNoSet);
		
		if(totalResults > 0) paginationCode = Pagination.getPaginationCodeForTree(pageSize, totalResults, pageNow);
		
		else paginationCode = "没有查询到有效数据！";
		
		branchTreeCode = this.branchService.getPageCode4BranchTree(false);
		
		request.setAttribute("branchId", branchId);
		
		request.setAttribute("titleOnQuery", titleOnQuery);
		
		request.setAttribute("modifyNewsPageNow", pageNow);
		
		pageNow = null;
		
		branchId = null;
		
		List<Branch> level2Branches = this.branchService.getBranchLevel2();
		boolean flag = false;
		Iterator<Branch> iter = level2Branches.iterator();
		while(iter.hasNext()){
			if(iter.next().getId() == focusBranchId){
				flag = true;
				break;
			}
		}
		if(!flag && level2Branches != null && level2Branches.size() > 0){
//			focusBranchId = level2Branches.get(0).getId();//开始院系风采的时候默认的不是第一个列表的分党委，而是 设置默认的是：北邮党委
			focusBranchId = 1;
		}
		
		request.setAttribute("focusBranchId", focusBranchId);
		request.setAttribute("level2Branches", level2Branches);
		
		return "manageCollegeNews";
	}
	
	public String newSpiritNews(){
		
		if(!this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL)) return "noPermission";
		
		return "newSpiritNews";
	}
	
	public String newSchoolNews(){
		
		if(!this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL)) return "noPermission";
		
		return "newSchoolNews";
	}
	
	public String newCollegeNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		HttpSession session = request.getSession();
		
		Admin session_admin = (Admin) session.getAttribute(SessionConstants.SESSION_USER);
		
		Admin admin = adminService.findAdminByAmdinNO(session_admin.getAdminNo());
		
		int branchId = admin.getAdminManageBranchId();
		
		//首先获得level 
		int level = (Integer) session.getAttribute(SessionConstants.LEVEL);
		
		if(level==SessionConstants.ADMIN_SECOND_LEVEL){//(2)分党委管理员
			
			Branch branch = branchService.findBranchById(branchId);
			firstPaList.add(branch);
		
		}else if(level==SessionConstants.ADMIN_THIRD_LEVEL){//(3)北邮校党委管理员
			
			//获得所有的分党委list
			firstPaList = branchService.findChildBranchesById(branchId);	
			
		}
		
		return "newCollegeNews";
	}
	
	public  String saveNewSpiritNews(){
		
		if(!this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL)) return "noPermission";
		
		Integer picTag = Integer.parseInt(request.getParameter("picTag"));
		if(title == null || title.trim().length() <= 0) return "error";
		if(content == null || content.trim().length() <= 0) return "error";
		
		HttpSession session = request.getSession();
		
		Admin admin = (Admin) session.getAttribute(SessionConstants.SESSION_USER);
		
		PartyNews bean = new PartyNews();
		bean.setTitle(title);
		bean.setContent(content);
		bean.setIsDelete(0);
		bean.setPosterNo(admin.getAdminNo());
		bean.setPostTime(new Timestamp(System.currentTimeMillis()));
		bean.setTag(PartyNews.Category_spirit_news);
		bean.setVisitedCount(0);
		bean.setPicTag(picTag);
		if(picTag == 1){
			
			if(newsPic == null ) return "error"; 
			
			String suffix = this.getNewsPicFileName().substring(this.getNewsPicFileName().lastIndexOf(".") + 1);
			String picFileName = this.createFilename(suffix);
			try {
				FileUtils.copyFile(newsPic, new File(PartyNewsAction.RealNewsPicPath+File.separator+picFileName));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			bean.setPicPath(PartyNewsAction.ServerNewsPicPath + "/" + picFileName);
		} 
		if(this.partyNewsService.saveOrUpdateSpiritNews(bean))
		{
			request.setAttribute("tag", PartyNews.Category_spirit_news);
			return "saveNewSpiritNews";
		}
			
		else return "error";
	}
	
	
	public String saveNewSchoolNews(){
		
		if(!this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL)) return "noPermission";
		
		Integer picTag = Integer.parseInt(request.getParameter("picTag"));
		
		if(title == null || title.trim().length() <= 0) return "error";
		if(content == null || content.trim().length() <= 0) return "error";
		
		HttpSession session = request.getSession();
		
		Admin admin = (Admin) session.getAttribute(SessionConstants.SESSION_USER);
		
		PartyNews bean = new PartyNews();
		bean.setTitle(title);
		bean.setContent(content);
		bean.setIsDelete(0);
		bean.setPosterNo(admin.getAdminNo());
		bean.setPostTime(new Timestamp(System.currentTimeMillis()));
		bean.setTag(PartyNews.Category_school_news);
		bean.setVisitedCount(0);
		bean.setPicTag(picTag);
		
		if(picTag == 1){
			
			if(newsPic == null ) return "error"; 
			
			String suffix = this.getNewsPicFileName().substring(this.getNewsPicFileName().lastIndexOf(".") + 1);
			String picFileName = this.createFilename(suffix);
			try {
				FileUtils.copyFile(newsPic, new File(PartyNewsAction.RealNewsPicPath+File.separator+picFileName));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			bean.setPicPath(PartyNewsAction.ServerNewsPicPath + "/" + picFileName);
		}
		
		if(this.partyNewsService.saveOrUpdateSpiritNews(bean)){
			request.setAttribute("tag", PartyNews.Category_school_news);
			return "saveNewSchoolNews";
		}
		else return "error";
	}
	
	public String saveNewCollegeNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		if(title == null || title.trim().length() <= 0) return "error";
		if(content == null || content.trim().length() <= 0) return "error";
		
		HttpSession session = request.getSession();
		
		Admin admin = (Admin) session.getAttribute(SessionConstants.SESSION_USER);
		
		if(admin.getAdminLevel() == SessionConstants.ADMIN_THIRD_LEVEL){ 
			
		if(firstPa == null)	return "error"; 
		
		List<Branch> collegesBranches = this.branchService.getBranchLevel2();
		
		List<Integer> level2BranchIds = new ArrayList<Integer>();
		
		for(Branch b : collegesBranches){
			
			level2BranchIds.add(b.getId());
		}
		
		if(!level2BranchIds.contains(firstPa)) return "error";
		}
		PartyNews bean = new PartyNews();
		bean.setTitle(title);
		bean.setContent(content);
		
		if(admin.getAdminLevel() == SessionConstants.ADMIN_THIRD_LEVEL)	bean.setBranchId(firstPa);
		else bean.setBranchId(admin.getAdminManageBranchId());
		
		bean.setIsDelete(0);
		bean.setPosterNo(admin.getAdminNo());
		bean.setPostTime(new Timestamp(System.currentTimeMillis()));
		bean.setTag(PartyNews.Category_college_news);
		bean.setVisitedCount(0);
		bean.setPicTag(0);
		if(this.partyNewsService.saveOrUpdateSpiritNews(bean)){
			
			request.setAttribute("tag", PartyNews.Category_college_news);
			
			if(admin.getAdminLevel() == SessionConstants.ADMIN_THIRD_LEVEL) request.setAttribute("collegeNewsBranchId", firstPa);
			
			else request.setAttribute("collegeNewsBranchId", admin.getAdminManageBranchId());
			
			return "saveNewCollegeNews";
		}
		else return "error";
	}
	
	public String deleteNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		Integer id = (request.getParameter("id") == null) ? null : Integer.parseInt(request.getParameter("id"));
		
		if(id == null) return "error";
		
		PartyNews news = this.partyNewsService.getNewsById(id);
		
		if(news == null) return "error";
		
		request.setAttribute("tag", news.getTag());
		
		request.setAttribute("collegeNewsBranchId",news.getBranchId());
		
		if(!this.partyNewsService.deleteNewsById(id)) return "error";
		
		return "deleteNews";
		
	}
	
	public String modifyNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		Integer id = (request.getParameter("id") == null) ? null : Integer.parseInt(request.getParameter("id"));
		
		int modifyNewsPageNow = Integer.parseInt(request.getParameter("pageNow"));
		
		request.setAttribute("modifyNewsPageNow", modifyNewsPageNow);
		
		if(id == null) return "error";
		
		this.currentNews = this.partyNewsService.getNewsById(id);
		
		if(currentNews == null) return "error";
		
		return "modifyNews";
	}
	
	public String modifyCollegeNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		Integer id = (request.getParameter("id") == null) ? null : Integer.parseInt(request.getParameter("id"));
		
		int modifyNewsPageNow = Integer.parseInt(request.getParameter("pageNow"));
		
		request.setAttribute("modifyNewsPageNow", modifyNewsPageNow);
		
		if(id == null) return "error";
		
		HttpSession session = request.getSession();
		Admin session_admin = (Admin) session.getAttribute(SessionConstants.SESSION_USER);
		
		Admin admin = adminService.findAdminByAmdinNO(session_admin.getAdminNo());
		int branchId = admin.getAdminManageBranchId();
		
		//首先获得level 
		int level = (Integer) session.getAttribute(SessionConstants.LEVEL);
		
		firstPaList.clear();
		
		this.currentNews = this.partyNewsService.getNewsById(id);
		
		if(currentNews == null) return "error";
		
		firstPa = currentNews.getBranchId();
		
		if(level==SessionConstants.ADMIN_FIRST_LEVEL){//(1)支部管理员
			//获得父亲的branchId(分党委id)
			
			Branch branch = branchService.getParentBranchById(branchId);//wangdong
			firstPaList.add(branch);
			
		}else if(level==SessionConstants.ADMIN_SECOND_LEVEL){//(2)分党委管理员
			
			Branch branch = branchService.findBranchById(branchId);
			firstPaList.add(branch);
		
		}else if(level==SessionConstants.ADMIN_THIRD_LEVEL){//(3)北邮校党委管理员
			
			//获得所有的分党委list
			firstPaList = branchService.findChildBranchesById(branchId);	
			
		}
		
		return "modifyCollegeNews";
	}
	
	public String saveModifyNews(){
		
		if(!(this.checkForAuth(SessionConstants.ADMIN_THIRD_LEVEL) || this.checkForAuth(SessionConstants.ADMIN_SECOND_LEVEL))) return "noPermission";
		
		HttpSession session = request.getSession();
		
		Admin session_admin = (Admin) session.getAttribute(SessionConstants.SESSION_USER);
		
		Admin admin = adminService.findAdminByAmdinNO(session_admin.getAdminNo());
		
		Integer id = (request.getParameter("id") == null) ? null : Integer.parseInt(request.getParameter("id"));
		
		int modifyNewsPageNow = Integer.parseInt(request.getParameter("modifyNewsPageNow"));
		
		request.setAttribute("modifyNewsPageNow", modifyNewsPageNow);
		
		if(id == null) return "error";
		
		this.currentNews = this.partyNewsService.getNewsById(id);
		
		if(currentNews == null ) return "error";
		
		Integer picTag = (request.getParameter("picTag") == null) ? null : Integer.parseInt(request.getParameter("picTag"));
		
		currentNews.setContent(content);
		currentNews.setTitle(title);
		
		if(currentNews.getTag() == 3 && admin.getAdminLevel().equals(SessionConstants.ADMIN_THIRD_LEVEL)){
			
			if(firstPa == -1) return "error";
			
			currentNews.setBranchId(firstPa);
		} 
		
		//如果是上级精神和学校要闻两种新闻的话需要修改是否为图片新闻的信息
		if(currentNews.getTag().equals(PartyNews.Category_spirit_news) || currentNews.getTag().equals(PartyNews.Category_school_news))
		{
			if(picTag == null) return "error"; 
			
			currentNews.setPicTag(picTag);
			
			//当上传图片不为空而且图片新闻标示是1的时候
			if(newsPic != null && picTag == 1){
				
				if(currentNews.getPicPath() != null && currentNews.getPicPath().trim() != "") {
					//如果原来已经是图片新闻，而且数据库和服务器上已经保存了记录和文件
					//就把原来的文件删掉
					try{
						File oldPicture = new File(ServletActionContext.getServletContext().getRealPath(currentNews.getPicPath()));
						FileUtils.copyFile(newsPic, oldPicture);
					}
					catch(Exception e){
						e.printStackTrace();
					}
				}
				else{
					//新建一个文件，同时把文件传到服务器，保存文件路径
					
					String suffix = this.getNewsPicFileName().substring(this.getNewsPicFileName().lastIndexOf(".") + 1);
					String picFileName = this.createFilename(suffix);
					try {
						FileUtils.copyFile(newsPic, new File(PartyNewsAction.RealNewsPicPath+File.separator+picFileName));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					currentNews.setPicPath(PartyNewsAction.ServerNewsPicPath + "/" + picFileName);
				}
			}
		}
		if(!partyNewsService.saveOrUpdateSpiritNews(currentNews)) return "error";
		
		//添加发布成功操作类别，用来在postSuccess.jsp表示操作成功的标志，以达到向不同的页面跳转的功能
		//（注：请和下面语句一起修改和删除）
		//修改新闻成功的标志是4
		request.setAttribute("tag", currentNews.getTag());
		request.setAttribute("collegeNewsBranchId", currentNews.getBranchId());
		newsPic = null;
		return "saveModifyNews";
	}
	
	public  String createFilename(String suffix)
	{
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		
		String filename = year + "_" + month + "_"+ day + "_" + hour + "_" + minute + "_" + second + "_newPic." + suffix;
		return filename;
	}
	
	public Map<String,String> getNameByAdminNo(Set<String> adminNoSet){
		
		if( adminNoSet == null || adminNoSet.size() <= 0) return null;
		
		Map<String,String> result = new HashMap<String,String>();
		
		for(String adminNo:adminNoSet){
			
			String name = this.adminService.findAdminNameByAdminNO(adminNo);
			
			if(name == null) name="";
				
			result.put(adminNo, name);
		}
		
		return result;
	}
	
	public boolean checkForAuth(int requiredLevel){
		
		HttpSession session = request.getSession();
		
		Object user = session.getAttribute(SessionConstants.SESSION_USER); 
		
		if(!(user instanceof Admin)) return false;
		
		Admin admin	 = (Admin) user;
		
		return admin.getAdminLevel() == requiredLevel;
	}
	
	public Map<String, String> getAdminNoToNameMap() {
		return adminNoToNameMap;
	}

	public void setAdminNoToNameMap(Map<String, String> adminNoToNameMap) {
		this.adminNoToNameMap = adminNoToNameMap;
	}

	public IPartyNewsService getPartyNewsService() {
		return partyNewsService;
	}
	public void setPartyNewsService(IPartyNewsService partyNewsService) {
		this.partyNewsService = partyNewsService;
	}
	public List<PartyNews> getPartyNews() {
		return partyNews;
	}
	public void setPartyNews(List<PartyNews> partyNews) {
		this.partyNews = partyNews;
	}
	public String getPaginationCode() {
		return paginationCode;
	}
	public void setPaginationCode(String paginationCode) {
		this.paginationCode = paginationCode;
	}

	public String getNewsCategory() {
		return newsCategory;
	}

	public void setNewsCategory(String newsCategory) {
		this.newsCategory = newsCategory;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public PartyNews getCurrentNews() {
		return currentNews;
	}

	public void setCurrentNews(PartyNews currentNews) {
		this.currentNews = currentNews;
	}

	public String getTitleOnQuery() {
		return titleOnQuery;
	}

	public void setTitleOnQuery(String titleOnQuery) {
		this.titleOnQuery = titleOnQuery;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public File getNewsPic() {
		return newsPic;
	}

	public void setNewsPic(File newsPic) {
		this.newsPic = newsPic;
	}

	public String getNewsPicFileName() {
		return newsPicFileName;
	}

	public void setNewsPicFileName(String newsPicFileName) {
		this.newsPicFileName = newsPicFileName;
	}

	public String getNewsPicContentType() {
		return newsPicContentType;
	}

	public void setNewsPicContentType(String newsPicContentType) {
		this.newsPicContentType = newsPicContentType;
	}

	public IAdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response = arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}
	
	public String getPosterNameByNo(String no){
		
		if(no == null || no == "") return "";
		
		return adminService.findAdminNameByAdminNO(no);
		
	}


	public String getBranchTreeCode() {
		return branchTreeCode;
	}


	public void setBranchTreeCode(String branchTreeCode) {
		this.branchTreeCode = branchTreeCode;
	}


	public IBranchService getBranchService() {
		return branchService;
	}


	public void setBranchService(IBranchService branchService) {
		this.branchService = branchService;
	}


	public Integer getBranchId() {
		return branchId;
	}


	public void setBranchId(Integer branchId) {
		this.branchId = branchId;
	}


	public List<Branch> getFirstPaList() {
		return firstPaList;
	}


	public void setFirstPaList(List<Branch> firstPaList) {
		this.firstPaList = firstPaList;
	}

	public Integer getFirstPa() {
		return firstPa;
	}


	public void setFirstPa(Integer firstPa) {
		this.firstPa = firstPa;
	}
	public String getBranchNameByBranchId(Integer id){
		
		if(id == null || id == -1) return "";
		
		String name = branchService.getBranchName(id);
		
		if(name == null) return "";
		
		return name;
		
	}


	public Integer getTag() {
		return tag;
	}


	public void setTag(Integer tag) {
		this.tag = tag;
	}
}
